package com.lgy.ms.service.controller;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import com.lgy.ms.core.exception.BusinessException;
import com.lgy.ms.core.utils.RequestHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class BaseController {

    /**
     * 把request封装成map
     */
    protected Map<String, Object> change2Map() throws BusinessException {
        HttpServletRequest request = RequestHolder.getRequest();
        Map<String, Object> reqMap = Maps.newHashMap();
        Map<String, String[]> tmp = request.getParameterMap();
        if (tmp != null) {
            for (String key : tmp.keySet()) {
                String[] values = tmp.get(key);
                if (StrUtil.isNotEmpty(values[0].trim())) {
                    reqMap.put(key, values[0].trim());
                }
            }
        }
        String dateRange = (String) reqMap.get("dateRange");
        if (StrUtil.isNotEmpty(dateRange)) {
            String[] strs = dateRange.split(" - ");
            reqMap.put("startDate", strs[0].replace("-", ""));
            reqMap.put("endDate", strs[1].replace("-", ""));
        }
        return reqMap;
    }
}
